package com.cz.zslu.controller;


import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.OnlineIM;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStore;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslStoreService;
import com.cz.zslu.service.OnlineIMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 在线留言 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-06-05
 */
@RestController
@RequestMapping("/sys/online")
@Api(description = "在线留言管理")
public class OnlineIMController extends BaseController<OnlineIMService, OnlineIM> {

    @Autowired
    private OnlineIMService onlineIMService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IZslStoreService zslStoreService;


    @ApiOperation("获取店铺全部留言分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public ReturnObject<List<OnlineIM>> getPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                @ApiParam("消息类型 重要提醒值为9（1、商品审核失败，2、开通旺铺审核失败，3、公司信息审核失败，4、产品续费）" +
                                                        "5、账号消息 6、投诉举报 7、其他消息，8、留言消息，10、系统公告）") @RequestParam Integer type) {

        EntityWrapper<OnlineIM> ew = new EntityWrapper<>();
        if (type != null) {
            if (type == 9) {
                List<Integer> list = new ArrayList<Integer>();
                list.add(1);
                list.add(2);
                list.add(3);
                list.add(4);
                ew.in("type", list);
            } else {
                ew.eq("type", type);
            }
        }
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        ew.eq("store_id", sysUser.getStoreId());
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        Page<OnlineIM> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("通过id获取详情")
//    @IgnoreAuth
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<OnlineIM> findInfoById(@ApiParam("id") @RequestParam Long id) {
        OnlineIM orgin = baseService.selectById(id);

        return new ReturnObject<>(0, "获取成功", orgin);
    }


    @ApiOperation("新增")
    @RequestMapping(value = "/saveIM", method = RequestMethod.POST)
    public ReturnObject<OnlineIM> saveIM(@ApiParam("姓名") @RequestParam String name,
                                         @ApiParam("联系电话") @RequestParam String phone,
                                         @ApiParam("QQ") @RequestParam String qq,
                                         @ApiParam("内容") @RequestParam String content,
                                         @ApiParam("商铺id") @RequestParam Long storeId,
                                         @ApiParam("消息类型") @RequestParam Integer type
    ) {
        SysUser fromSysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OnlineIM info = new OnlineIM();
        info.setType(type);
        info.setName(name);
        if (StringUtils.isEmpty(name)) {
            info.setName(fromSysUser.getUsername());
        }
        info.setFromUserId(BaseContextHandler.getUserID());
        info.setHeadPic(fromSysUser.getHeadPortraits());
        info.setPhone(phone);
        if (StringUtils.isEmpty(phone)) {
            info.setPhone(fromSysUser.getPhone());
        }
        info.setQq(qq);
        info.setContent(content);
        if (storeId != null) {
            info.setStoreId(storeId);
            ZslStore store = zslStoreService.selectById(storeId);
            info.setStoreName(store.getName());
        }
        info.setIsDeal(0);
        baseService.insert(info);
        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("回复")
    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    public ReturnObject<OnlineIM> reply(@ApiParam("id") @RequestParam Long id,
                                        @ApiParam("内容") @RequestParam String replyContent) {
        OnlineIM oldOnLineIM = baseService.selectById(id);
        oldOnLineIM.setIsDeal(1);
        baseService.updateById(oldOnLineIM);
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OnlineIM info = new OnlineIM();
        info.setType(8);
        info.setName(sysUser.getUsername());
        info.setContent(replyContent);
        info.setPhone(sysUser.getPhone());
        info.setQq(oldOnLineIM.getQq());
        info.setFromUserId(sysUser.getId());
        info.setUserId(oldOnLineIM.getUserId());
        info.setHeadPic(sysUser.getHeadPortraits());
        info.setStoreId(oldOnLineIM.getStoreId());
        ZslStore store = zslStoreService.selectById(oldOnLineIM.getStoreId());
        info.setStoreName(store.getName());
        baseService.insert(info);

        return new ReturnObject<>(0, "回复成功", null);
    }

    @ApiOperation("处理")
    @RequestMapping(value = "/deal", method = RequestMethod.POST)
    public ReturnObject<OnlineIM> deal(@ApiParam("id") @RequestParam Long id) {
        OnlineIM info = baseService.selectById(id);
        info.setIsDeal(1);
        baseService.updateById(info);
        return new ReturnObject<>(0, "处理成功");
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject<OnlineIM> del(@ApiParam("id") @RequestParam Long id) {
        OnlineIM info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);
        return new ReturnObject<>(0, "删除成功");
    }

    @ApiOperation("获取店铺全部留言分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getAllPage", method = RequestMethod.POST)
    public ReturnObject<List<OnlineIM>> getAllPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                   @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                   @ApiParam("名字/内容") @RequestParam String name,
                                                   @ApiParam("是否处理 0、未处理 1、已处理") @RequestParam Integer isDeal) {

        EntityWrapper<OnlineIM> ew = new EntityWrapper<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(name)) {
            ew.andNew().like("name", name).or().like("content", name);
        }
        if (isDeal != null) {
            ew.eq("is_deal", isDeal);
        }
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        Page<OnlineIM> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取全部系统公告")
    @IgnoreAuth
    @RequestMapping(value = "/getNews", method = RequestMethod.POST)
    public ReturnObject<List<OnlineIM>> getNews() {

        EntityWrapper<OnlineIM> ew = new EntityWrapper<>();
        ew.eq("type", 10);
//        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
//        ew.eq("store_id", sysUser.getStoreId());
        ew.eq("state", 0);
//        ew.isNull("store_id");
        ew.orderBy("crt_time", false);
        List<OnlineIM> list= baseService.selectList(ew);
        return new ReturnObject<>(0, "获取成功",list);
    }


}

